<?php

/**
 * This code is part of the FileManager software (www.gerd-tentler.de/tools/filemanager), copyright by
 * Gerd Tentler. Obtain permission before selling this code or hosting it on a commercial website or
 * redistributing it over the Internet or in any other medium. In all cases copyright must remain intact.
 */

/**
 * This class provides static methods for general use.
 */
class Tools {

/* PUBLIC STATIC METHODS *********************************************************************** */

	/**
	 * delete files from local directory
	 *
	 * @param string $dir	directory
	 */
	function cleanDir($dir) {
		if($dp = @opendir($dir)) {
			while(($file = @readdir($dp)) !== false) {
				if($file != '.' && $file != '..') @unlink("$dir/$file");
			}
			@closedir($dp);
		}
	}

	/**
	 * get number of files in local directory
	 *
	 * @param string $dir	directory
	 * @return integer
	 */
	function getFileCount($dir) {
		$cnt = 0;
		if($dp = @opendir($dir)) {
			while(($file = @readdir($dp)) !== false) {
				if($file != '.' && $file != '..') $cnt++;
			}
			@closedir($dp);
		}
		return $cnt;
	}

	/**
	 * read local file
	 *
	 * @param string $file		file path
	 * @param string $encoding	optional: character set, example: ISO-8859-1
	 * @return string $data		file data
	 */
	function readLocalFile($file, $encoding = '') {
		$data = '';
		if($fp = @fopen($file, 'r')) {
			$data = @fread($fp, filesize($file));
			@fclose($fp);
		}
		return ($encoding != '') ? Tools::utf8Decode($data, $encoding) : $data;
	}

	/**
	 * save local file
	 *
	 * @param string $path		file path
	 * @param string $data		file data
	 * @param string $encoding	optional: character set, example: ISO-8859-1
	 * @return boolean
	 */
	function saveLocalFile($path, &$data, $encoding = '') {
		$ok = false;

		if($encoding != '') {
			$mode = 'wt';
			$data = Tools::utf8Decode($data, $encoding);
		}
		else $mode = 'w';

		if($fp = @fopen($path, $mode)) {
			$ok = @fwrite($fp, $data);
			@fclose($fp);
		}
		return $ok;
	}

	/**
	 * check if string contains UTF-8 characters
	 *
	 * @param string $str	the string
	 * @return boolean
	 */
	function isUtf8($str) {
		return preg_match('%(?:
			[\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
			|\xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
			|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
			|\xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
			|\xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
			|[\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
			|\xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
			)%x', $str);
	}

	/**
	 * decode string if it contains UTF-8 characters
	 *
	 * @param string $str		the string
	 * @param string $encoding	optional: character set, example: ISO-8859-1
	 * @return string			decoded string
	 */
	function utf8Decode($str, $encoding = '') {
		if(Tools::isUtf8($str) && $encoding != 'UTF-8') {
			if(function_exists('iconv')) {
				if($encoding == '') $encoding = 'ISO-8859-1';
				$str = iconv('UTF-8', "$encoding//TRANSLIT", $str);
			}
			else $str = utf8_decode($str);
		}
		return $str;
	}

	/**
	 * print HTML image tag
	 *
	 * @param string $path			image path
	 * @param integer $width		image width (pixels)
	 * @param integer $height		image height (pixels)
	 * @param string $action		optional: JavaScript action
	 * @param string $tooltip		optional: tooltip
	 * @param string $style			optional: CSS styles
	 */
	function printIcon($path, $width, $height, $action = '', $tooltip = '', $style = '') {
		print "<img src=\"$path\" border=\"0\" width=\"$width\" height=\"$height\"";
		if($action) print " onClick=\"$action\"";
		if($tooltip) print " alt=\"$tooltip\" title=\"$tooltip\"";
		if($style) print " style=\"$style\"";
		print "/>\n";
	}
}

?>
